import java.io.*;
import java.util.Arrays;

/**
 * Created by zy on 2019/5/5.
 */
public class CustomEncryption {

    //简单的操作加密解密,example outFile1
    public static void main(String a[]) throws IOException {
        //readTest();
        if (a.length > 0) {
            String[] split;
            split = a;
            for (int i = 0; i < split.length; i++) {
                if (split[i].equals("-d")) {
                    try {
                        de(split[i + 1]);
                    } catch (IOException e) {
                        if (e instanceof FileNotFoundException) {
                            System.out.print("文件" + split[i + 1] + "未找到");
                        } else e.printStackTrace();
                    }
                } else if (split[i].equals("-e")) {
                    try {
                        en(split[i + 1], split[i + 3]);
                    } catch (IOException e) {
                        if (e instanceof FileNotFoundException) {
                            System.out.print("文件" + split[i + 1] + "未找到");
                        } else e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void readTest() throws IOException {
        File file = new File("C:/Users/zy/Desktop/vps/account");
        FileInputStream stream = new FileInputStream(file);
        byte[] b = new byte[1024];
        while (true) {
            int s = stream.read(b, 0, b.length);
            if (s == -1) break;
            String en = encryption(new String(Arrays.copyOf(b, s)));
            System.out.println(en);
            System.out.println("<-   ->");
            System.out.println(decode(en));
        }
    }

    private static void de(String file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String str;
        while ((str = reader.readLine()) != null) {
            System.out.println(decode(str));
        }
        reader.close();
    }

    private static void en(String file, String outFile) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String str;
        BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
        while ((str = reader.readLine()) != null) {
            if (str.trim().equals(""))continue;
            writer.write(encryption(str) + '\n');
        }
        reader.close();
        writer.close();
    }

    private static String encryption(String con) {
        StringBuilder temp = new StringBuilder();
        int length = 1;
        for (char c : con.toCharArray()) {
            if (c > 128)
                temp.append(" ");
            temp.append((Integer.toHexString(c + length)));
            if (length >= 6) length = 1;
            length++;
        }
        return temp.toString();
    }

    private static String decode(String con) {
        StringBuilder temp = new StringBuilder();
        int length = 1;
        String sz[] = con.split(" ");
        for (int i = 0; i < sz.length; i++) {
            if (i == 0) {
                for (int i1 = 0; i1 < sz[0].toCharArray().length; ) {
                    String st = sz[0].substring(i1, i1 += 2);
                    char ch = (char) (Integer.parseInt(st, 16) - length);
                    if (ch == '\n') length = 1;
                    temp.append(ch);
                    if (length >= 6) length = 1;
                    length++;
                }
            } else {
                for (int i1 = 0; i1 < sz[i].toCharArray().length; ) {
                    String st;
                    if (i1 == 0) st = sz[i].substring(i1, i1 += 4);
                    else st = sz[i].substring(i1, i1 += 2);
                    char ch = (char) (Integer.parseInt(st, 16) - length);
                    if (ch == '\n') length = 1;
                    temp.append(ch);
                    if (length >= 6) length = 1;
                    length++;
                }
            }
        }
        //for (String c : con.split("0000")) {
        //    if (c.equals(""))continue;
        //    char ch = (char) (Integer.parseInt(c,16) - length);
        //    if (ch == '\n') length = 1;
        //    temp.append(ch);
        //    if (length >= 6) length = 1;
        //    length++;
        //}
        return temp.toString();
    }
}
